package com.david.elasticjob.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import com.david.elasticjob.domain.Person;
import com.david.elasticjob.service.PersonService;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author David
 * @version V1.0
 * @Package com.david.elasticjob
 * @date 2021/5/4 16:52
 */
@Component
public class PersonJob implements DataflowJob<Person> {

    private static final Logger LOGGER= LoggerFactory.getLogger(PersonJob.class);
    @Autowired
    private PersonService personService;
    @Value("${server.port}")
    private Integer port;

    @Override
    public List<Person> fetchData(ShardingContext shardingContext) {
        Integer sex = Integer.valueOf(shardingContext.getShardingParameter());
        List<Person> personList = personService.findPersonBySex(sex, 5);
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        LOGGER.info("抓取时间：{}，服务端口为：port:{},性别：{},数据抓取:personList:{}",now,port,sex==0?"女":"男",personList);
        return personList;
    }

    @Override
    public void processData(ShardingContext shardingContext, List<Person> data) {
        LOGGER.info("服务端口为：port:{},处理数据:data:{}",port,data);
        personService.doSomething(data);
    }
}
